package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"new_ginblog/internal/middleware"
	"new_ginblog/internal/models"
	"new_ginblog/internal/repositories"
	"new_ginblog/internal/utils/errormsg"
)

func Login(c *gin.Context) {
	var user models.User
	_ = c.ShouldBindJSON(&user)
	var accessToken, refreshToken string

	// 查看当前账号是否正确
	statusCode = repositories.CheckLogin(user.Username, user.Password)
	user.Role = repositories.GetRole(user.Username)
	RoleString := repositories.GetRoleString(user.Role)

	if statusCode == errormsg.SUCCESS {
		accessToken, _ = middleware.SetAccessToken(user.Username)
		refreshToken, _ = middleware.SetRefreshToken(user.Username)
	}

	c.JSON(http.StatusOK, gin.H{
		"code":          statusCode,
		"msg":           errormsg.GetErrorMsg(statusCode),
		"role":          RoleString,
		"access_token":  accessToken,
		"refresh_token": refreshToken,
	})
}
